package com.dmbjz.coredemo.service;


import com.dmbjz.coredemo.event.LoginSuccessEvent;
import com.dmbjz.coredemo.po.UserPo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

//通过类实现的监听时间，Order 只能写在类上
@Order(1)
@Service
@Slf4j
public class AccountService implements ApplicationListener<LoginSuccessEvent> {


    /**
     * 账号服务自动签到
     */
    public void addAccount(String userName) {
        try {
            TimeUnit.SECONDS.sleep(2);
            log.info("用户:{} 签到成功", userName);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 监听事件方式，进行业务处理
     *
     * @param event 需要监听的事件
     */
    @Override
    public void onApplicationEvent(LoginSuccessEvent event) {

        UserPo source = (UserPo) event.getSource();
        log.info("Account2Service 感知到 LoginSuccessEvent 事件，获取到的数据：{}", source);
        addAccount(source.getUserName());

    }

}
